<?php
class Core_Model_Google  implements Core_Model_Mainable{
    
    public function main(array $variables = null) {
        require_once "./openid/openid.php";
        try {
            $openid = new LightOpenID($_SERVER['HTTP_HOST']);
            //Not already logged in
            if (!$openid->mode) {
		//The google openid url
		$openid->identity = 'https://www.google.com/accounts/o8/id';

		// Information for for the user
		$openid->required = array('contact/email', 'namePerson/first', 'namePerson/last');

		// redirect to google
                echo '<meta http-equiv="Refresh" content="0;url='.$openid->authUrl().'">';
		
            } elseif ($openid->mode == 'cancel') {
		// user cancel the login
		// redirect may be ?
            } else {
		if ($openid->validate()) {
			//User logged in
			$variables['google'] = $openid->getAttributes();
                        $_SESSION['google'] = $variables;
			$first_name = $variables['google']['namePerson/first'];
			$last_name = $variables['google']['namePerson/last'];
			$email = $variables['google']['contact/email'];
			return $variables;
			// do your login

		} else {
			//user is not logged in
		}
            }
        } catch(Exception $e) {
	// something went wrong
	echo $e->getMessage();
	exit;
        }
    }

}
